package com.yi.servicepassengeruser.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yi.internalcommon.constant.CommonStatusEnum;
import com.yi.internalcommon.dto.PassengerUser;
import com.yi.internalcommon.dto.ResponseResult;
import com.yi.servicepassengeruser.mapper.PassengerUserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserService {
    @Autowired
    private PassengerUserMapper mapper;
    public ResponseResult  loginOrRegister(String passengerPhone){
        //根据手机号查询用户信息
        LambdaQueryWrapper<PassengerUser> lqw=new LambdaQueryWrapper();
        lqw.eq(StringUtils.isNotEmpty(passengerPhone),PassengerUser::getPassengerPhone,passengerPhone);
        List<PassengerUser> passengerUsers = mapper.selectList(lqw);
        //判断用户信息是否存在
        if(passengerUsers.size()==0) {
            //不存在则插入
            PassengerUser passengerUser = new PassengerUser();
            passengerUser.setPassengerName("张三");
            passengerUser.setPassengerGender((byte) 0);
            passengerUser.setPassengerPhone(passengerPhone);
            passengerUser.setState((byte) 0);
            passengerUser.setGmtCreate(LocalDateTime.now());
            passengerUser.setGmtModified(LocalDateTime.now());
            mapper.insert(passengerUser);
        }
    return ResponseResult.success();
    }

    /**
     * 根据手机号查询用户信息
     * @param passengerPhone
     * @return
     */
    public ResponseResult getUserByPhone(String passengerPhone){
        //根据手机号查询用户信息
        LambdaQueryWrapper<PassengerUser> lqw=new LambdaQueryWrapper();
        lqw.eq(StringUtils.isNotEmpty(passengerPhone),PassengerUser::getPassengerPhone,passengerPhone);
        PassengerUser passengerUser = mapper.selectOne(lqw);
        if(passengerUser==null){
            return  ResponseResult.fail(CommonStatusEnum.USER_NOT_EXISTS.getCode(),CommonStatusEnum.USER_NOT_EXISTS.getValue());
        }
        return  ResponseResult.success(passengerUser);
    }
}
